// Copyright 2009 sbva project
//
// This file is part of sbva,
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.googlecode.sbva;

import com.googlecode.sbva.PropertyAndErrorMessages.ErrorMessages;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * This is a struts independent class for holding error messages 
 * created by violation of bean validaion constraints.  When configuring
 * constraints in user code to be fully compatible with sbvs, use the struts
 * scheme svba uses that is described {@link StrutsBeanValidationAdapter here}.
 * 
 */
public class PropertyAndErrorMessages extends HashMap<String, ErrorMessages> {

   /**
    *
    * Stores all error messages associated with a single bean property
    * that is being validated.
    *
    * This is a subclass of {@link java.util.ArrayList ArrayList} that holds
    * {@link ErrorMessage ErrorMessage}.
    *
    */
   public static class ErrorMessages extends ArrayList<ErrorMessage> {

      public ErrorMessages() {
      }

      public ErrorMessages(String errorKey) {

         ErrorMessage em = new ErrorMessage(errorKey);
         add(em);

      }

      public ErrorMessages(String errorKey, String[] errorParams) {

         ErrorMessage em = new ErrorMessage(errorKey, errorParams);
         add(em);

      }

      public void add(String errorKey, String[] errorParams) {

         this.add(new ErrorMessage(errorKey, errorParams));

      }
   }

   /**
    *
    * This contains the message key and any messages associtaied
    * with that message key.
    *
    */
   public static class ErrorMessage {

      String errorKey;
      String[] errorParams;

      public ErrorMessage(String errorKey) {

         this.errorKey = errorKey;
         this.errorParams = null;

      }

      public ErrorMessage(String errorKey, String[] errorParams) {

         this.errorKey = errorKey;
         this.errorParams = errorParams;

      }

      public String getErrorKey() {
         return errorKey;
      }

      public String[] getErrorParameters() {
         return errorParams;
      }
   }

   /**
    *
    * corresponds to the html input propery that was the source of this data
    *
    */
   public Set<String> getProperties() {

      return keySet();

   }

   /**
    *
    * corresponds to the html input propery that was the source of this data
    *
    */
   public ErrorMessages lookupErrorMessages(String property) {

      return get(property);

   }

   public void add(String propertyName, String errorKey) {

      add(propertyName, errorKey, null);
   }

   public void add(String propertyName, String errorKey, String[] errorParams) {

      ErrorMessages errorMessages = get(propertyName);
      if (errorMessages == null) {
         errorMessages = new ErrorMessages();
         put(propertyName, errorMessages);
      }

      errorMessages.add(new ErrorMessage(errorKey, errorParams));


   }

}
